package com.vip.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.jwt.entity.Permission;
import com.vip.jwt.entity.RolePermission;
import com.vip.jwt.mapper.PermissionMapper;
import com.vip.jwt.mapper.RolePermissionMapper;
import com.vip.jwt.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限关联表 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public int deleteByRoleId(String roleId) {
        QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",roleId);
        return rolePermissionMapper.delete(rolePermissionQueryWrapper);
    }

    @Override
    public List<String> findByRoleId(String roleId) {
        QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",roleId);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(rolePermissionQueryWrapper);
        List<Permission> permissions = permissionMapper.selectList(null);
        List<Permission> findPermission = RolePermission.findByIds(permissions, rolePermissions);
        return findPermission.stream()
                .map(Permission::getId)
                .collect(Collectors.toList());
    }

}
